package com.yxzr.youyou;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.yxzr.youyou.base.BaseActivity;
import com.yxzr.youyou.retrofit.BaseObserver;
import com.yxzr.youyou.retrofit.RequestUtil;
import com.yxzr.youyou.utils.SharedPreferencesHelper;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class RechargeCardActivity extends BaseActivity {
    private EditText rechargecard_et_content;
    private Button rechargecard_btn;
    private SharedPreferencesHelper sp;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rechargecard);
        sp = new SharedPreferencesHelper(this,"logindata");
        setTitleWithBack();
        initView();
    }

    private void initView() {
        rechargecard_et_content = findViewById(R.id.rechargecard_et_content);
        rechargecard_btn = findViewById(R.id.rechargecard_btn);

        rechargecard_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData();
            }
        });
    }


    private void initData(){
        if (TextUtils.isEmpty(rechargecard_et_content.getText().toString().trim())){
            Toast.makeText(RechargeCardActivity.this,"请输入充值卡key",Toast.LENGTH_LONG).show();
            return;
        }
        RequestUtil.getInstence().API().rechargeCard((String) sp.getSharedPreference("token",""),
                rechargecard_et_content.getText().toString().trim())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(this,true){
                    @Override
                    protected void onSuccees(String t) {
                        Toast.makeText(RechargeCardActivity.this,"兑换成功",Toast.LENGTH_LONG).show();
                        finish();
                    }

                    @Override
                    protected void onFail(int error_code) {


                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }
}
